package com.spzx.common.exception;

import com.spzx.model.vo.common.Result;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Controller层统一异常捕获
 * Project: spzx-parent
 * Package: com.spzx.common.exception
 * Version: 1.0
 * <p>
 * Created by  wangjiaxin  on 2023/11/30 22:55
 */
@ControllerAdvice
public class GlobalExceptionHandler {

    //全局异常处理
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Result error(Exception e) {
        e.printStackTrace();
        return Result.build(null, 201, "出现了异常");
    }

    //自定义异常处理类
    @ExceptionHandler(value = MyException.class)     // 处理自定义异常
    @ResponseBody
    public Result error(MyException exception) {
        exception.printStackTrace();
        return Result.build(null, exception.getResultCodeEnum());
    }
}
